package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.CampusContact;
import com.ruoyi.system.domain.CampusNavigation;
import com.ruoyi.system.domain.HospitalCampus;
import com.ruoyi.system.mapper.CampusContactMapper;
import com.ruoyi.system.mapper.CampusNavigationMapper;
import com.ruoyi.system.mapper.HospitalCampusMapper;
import com.ruoyi.system.service.HospitalCampusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class HospitalCampusServiceImpl implements HospitalCampusService {
    @Autowired
    private HospitalCampusMapper campusMapper;

    @Autowired
    private CampusContactMapper contactMapper;

    @Autowired
    private CampusNavigationMapper navigationMapper;



    @Override
    public List<HospitalCampus> listAll() {
        return campusMapper.selectAll();
    }

    @Override
    public HospitalCampus selectById(Long id) {
        return campusMapper.selectById(id);
    }

    @Override
    @Transactional
    public int save(HospitalCampus campus) {
        int result = campusMapper.insert(campus);
        Long campusId = campus.getId();

        // 保存联系电话
        if (campus.getContacts() != null) {
            for (CampusContact contact : campus.getContacts()) {
                contact.setCampusId(campusId);
                contactMapper.insert(contact);
            }
        }

        // 保存导航信息
        if (campus.getNavigations() != null) {
            for (CampusNavigation navigation : campus.getNavigations()) {
                navigation.setCampusId(campusId);
                navigationMapper.insert(navigation);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public int update(HospitalCampus campus) {
        // 先更新主表
        int result = campusMapper.update(campus);
        Long campusId = campus.getId();

        // 删除原有的联系电话和导航信息
        contactMapper.deleteByCampusId(campusId);
        navigationMapper.deleteByCampusId(campusId);

        // 保存新的联系电话
        if (campus.getContacts() != null) {
            for (CampusContact contact : campus.getContacts()) {
                contact.setCampusId(campusId);
                contactMapper.insert(contact);
            }
        }

        // 保存新的导航信息
        if (campus.getNavigations() != null) {
            for (CampusNavigation navigation : campus.getNavigations()) {
                navigation.setCampusId(campusId);
                navigationMapper.insert(navigation);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public int delete(Long id) {
        // 先删除关联表数据
        contactMapper.deleteByCampusId(id);
        navigationMapper.deleteByCampusId(id);

        // 再删除主表数据
        return campusMapper.delete(id);
    }

    @Override
    public HospitalCampus getByIdWithContacts(Long id) {
        return campusMapper.selectByIdWithContacts(id);
    }

    @Override
    public List<HospitalCampus> selectAllWithContactsAndNavigations(String campusName) {
        return campusMapper.selectAllWithContactsAndNavigations(campusName);
    }

    @Override
    public int selectIdByName(String campusName) {
        return campusMapper.selectIdByName(campusName);
    }
}
